 package com.weiyuwqj.admin.controller.admin.sys;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.api.service.BasCouponService;
import com.weiyuwqj.api.service.BasTicketService;
import com.weiyuwqj.api.service.SysConfigTicketRuleService;
import com.weiyuwqj.api.service.SysConfigTicketService;
import com.weiyuwqj.api.service.SysConfigTicketUserService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.SysConfigTicket;
import com.weiyuwqj.api.model.SysConfigTicketRule;
import com.weiyuwqj.api.model.SysConfigTicketUser;

import com.weiyuwqj.api.model.convert.CouponStatusConvert;
import com.weiyuwqj.api.model.convert.InvestAddtimeConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BasCouponVo;
import com.weiyuwqj.api.model.vo.BasTicketVo;
import com.weiyuwqj.api.util.ExcelUtils;
import com.weiyuwqj.api.util.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by huzj on 2016/8/25.
 */
@Controller
@RequestMapping(value = "/sys/ticketRule")
public class SysConfigTicketRuleController extends BaseController {
    @Autowired
    SysConfigTicketRuleService ticketRuleService;

    @Autowired
    SysConfigTicketService configTicketService;

    @Autowired
    BasCouponService couponService;

    @Autowired
    BasTicketService ticketService;

    @Autowired
    SysConfigTicketUserService ticketUserService;

    @Autowired
    BasUserService basUserService;

    //获取红包规则分页列表
    @ResponseBody
    @RequestMapping(value = "/getCouRuleList", method = RequestMethod.POST)
    public Object getCouRuleList(Paginator p, String name, String beginDateTime, String endDateTime) {
        Map<String, Object> map = new HashMap();
        if (StringUtils.isNotEmpty(name)) {
            map.put("name", name);
        }

        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        return ticketRuleService.getCouponList(p, map);
    }

    //获取加息券规则分页列表
    @ResponseBody
    @RequestMapping(value = "/getTicRuleList", method = RequestMethod.POST)
    public Object getTicRuleList(Paginator p, String name, String beginDateTime, String endDateTime) {
        Map<String, Object> map = new HashMap();
        if (StringUtils.isNotEmpty(name)) {
            map.put("name", name);
        }
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        return ticketRuleService.getTicketList(p, map);
    }


    //添加卡劵规则
    @ResponseBody
    @RequestMapping(value = "/addRule", method = RequestMethod.POST)
    public Map addRule(SysConfigTicketRule rule) {
        Integer sysUserId=this.getSessionUserId();
        rule.setStatus(0);
        rule.setSysUserId(sysUserId);
        Date now = new Date();
        rule.setAddtime(now);
        rule.setAddip(IpUtils.get());
        Integer ruleId =ticketRuleService.insertRule(rule);
        Map map = new HashMap();
        map.put("result","success");
        map.put("ruleId",ruleId);
        return map;
    }

    //添加卡劵配置
    @ResponseBody
    @RequestMapping(value = "/addConfigTicket", method = RequestMethod.POST)
    public Map addConfigTicket(SysConfigTicket configTicket) {
        configTicket.setStatus(1);
        configTicket.setAddtime(new Date());
        configTicketService.insertConfigTicket(configTicket);
        return Message.OK;
    }

    //添加关联用户
    @ResponseBody
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Object addUser(String userIdLink,Integer ruleId) {

        String[] userIdArray=userIdLink.split(",");
        List<SysConfigTicketUser> userList = new ArrayList<SysConfigTicketUser>();
        for (String userIdStr:userIdArray){
            SysConfigTicketUser ticketUser = new SysConfigTicketUser();
            ticketUser.setUserId(Integer.parseInt(userIdStr));
            userList.add(ticketUser);
        }
        ticketUserService.insert(userList,ruleId);
        return Message.OK;
    }

    //查看此规则对应领用红包的列表
    @ResponseBody
    @RequestMapping(value = "/listCouponByRuleId", method = RequestMethod.POST)
    public Object listCouponByRuleId(Paginator p, Integer ruleId) {
        Map map = new HashMap();
        map.put("ruleId", ruleId);
        return couponService.getList(p, map);
    }

    //查看此规则对应领用加息劵的列表
    @ResponseBody
    @RequestMapping(value = "/listTicketByRuleId", method = RequestMethod.POST)
    public Object listTicketByRuleId(Paginator p, Integer ruleId) {
        Map map = new HashMap();
        map.put("ruleId", ruleId);
        return ticketService.getList(p, map);
    }

    //查看此规则所对应的用户
    @ResponseBody
    @RequestMapping(value = "/listUserByRuleId", method = RequestMethod.POST)
    public Object listUserByRuleId(Integer ruleId) {
        Map map = new HashMap();
        map.put("ruleId", ruleId);
        return basUserService.listUserInvest(map);
    }

    //获取此规则对应的可用红包内容
    @ResponseBody
    @RequestMapping(value = "/listConfigCoupon", method = RequestMethod.POST)
    public Object listConfigCoupon(Integer ruleId) {
        Map map = new HashMap();
        map.put("ruleId",ruleId);
        map.put("status",1);
        return configTicketService.getList(map);
    }

    //获取此规则对应的可用加息劵内容
    @ResponseBody
    @RequestMapping(value = "/listConfigTicket", method = RequestMethod.POST)
    public Object listConfigTicket(Integer ruleId) {
        Map map = new HashMap();
        map.put("ruleId",ruleId);
        map.put("status",1);
        return configTicketService.getList(map);
    }

    //获取单个规则
    @ResponseBody
    @RequestMapping(value = "/getSingle", method = RequestMethod.POST)
    public Object getSingle(Integer id) {
        return ticketRuleService.selectByKey(id);
    }

    //更改状态
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public Object updateStatus(Integer ruleId,Integer newStatus) {
        if(newStatus!=null ){
            ticketRuleService.updateStatus(ruleId,newStatus);
        }
        return Message.OK;
    }

    //获取用户列表
    @ResponseBody
    @RequestMapping(value = "/listUser", method = RequestMethod.POST)
    public Object listUser(String beginInvestTime,
                           String endInvestTime,
                           String minInvestTotalAmount,
                           String maxInvestTotalAmount,
                           String singleInvestAmout,
                           Integer ruleId) {
        Map map = new HashMap();
        if(StringUtils.isNotEmpty(beginInvestTime)){
            map.put("beginInvestTime",beginInvestTime);
        }
        if(StringUtils.isNotEmpty(endInvestTime)){
            map.put("endInvestTime",endInvestTime);
        }
        if(StringUtils.isNotEmpty(maxInvestTotalAmount)){
            map.put("maxInvestTotalAmount",maxInvestTotalAmount);
        }
        if(StringUtils.isNotEmpty(minInvestTotalAmount)){
            map.put("minInvestTotalAmount",minInvestTotalAmount);
        }
        if(StringUtils.isNotEmpty(singleInvestAmout)){
            map.put("singleInvestAmout",singleInvestAmout);
        }
        if(ruleId !=null && ruleId>0){
            map.put("ruleId",ruleId);
        }
        return basUserService.listUserInvest(map);
    }

    //获取上传用户列表
    @ResponseBody
    @RequestMapping(value = "/listUploadUser", method = RequestMethod.POST)
    public Object listUploadUser(MultipartFile file) {
        List<List> list =null;
        if (file ==null){
            return list;
        }else {
            InputStream is = null;
            try {
                is = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path = file.getOriginalFilename();
            list = ExcelUtils.readExcel(is, path, 1);
        }
        List<String> userNameList = new ArrayList<String>();
        if (list == null || list.size() == 0) {
            return null;
        }
        for (List cellList : list) {
            for (Object obj : cellList) {
                userNameList.add((String) obj);
            }
        }
        return basUserService.listByUserName(userNameList);
    }

    //根据输入获取用户列表
    @ResponseBody
    @RequestMapping(value = "/listInputUser", method = RequestMethod.POST)
    public Object listInputUser(String  userNames) {
        if(userNames == null){
            userNames = "";
        }
        String userNameArray[] = userNames.split(",");
        List<String> list = Arrays.asList(userNameArray);
        return basUserService.listByUserName(list);
    }

    //根据邀请人获取用户列表
    @ResponseBody
    @RequestMapping(value = "/listInviteUser", method = RequestMethod.POST)
    public Object listInviteUser(MultipartFile file) {
        if (file ==null){
            return null;
        }
        InputStream is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String path = file.getOriginalFilename();
        List<List> list = ExcelUtils.readExcel(is, path, 1);
        List<String> inviteUserNameList = new ArrayList<String>();
        if (list == null || list.size() == 0) {
            return null;
        }
        for (List cellList : list) {
            for (Object obj : cellList) {
                inviteUserNameList.add((String) obj);
            }
        }
        return basUserService.listByInviteUser(inviteUserNameList);
    }

    //根据输入邀请人获取用户列表
    @ResponseBody
    @RequestMapping(value = "/listInputInvite", method = RequestMethod.POST)
    public Object listInputInvite(String inviteUserNames) {
        if(inviteUserNames == null){
            inviteUserNames= "";
        }
        String userNameArray[] = inviteUserNames.split(",");
        List<String> list = Arrays.asList(userNameArray);
        return basUserService.listByInviteUser(list);
    }


    //修改规则
    @ResponseBody
    @RequestMapping(value = "/updateRule", method = RequestMethod.POST)
    public Object updateRule(SysConfigTicketRule rule) {
        ticketRuleService.updateRule(rule);
        return Message.OK;
    }


    //修改内容
    @ResponseBody
    @RequestMapping(value = "/updateConfigTicket", method = RequestMethod.POST)
    public Object updateConfigTicket(SysConfigTicket configTicket) {
        configTicketService.updateNotNull(configTicket);
        return Message.OK;
    }



    //导出领用的红包的使用记录
    @ResponseBody
    @RequestMapping(value = "/excel/couponByRuleId", method = RequestMethod.POST)
    public Object excelCoupon(Integer ruleId,HttpServletRequest request) {
        String[] cArray={"用户名","状态","红包金额","领用时间","使用时间","投资项目","投资金额"};
        String[] eArray={"Username","Status","Amount","Addtime","UseTime","ItemName","InvestDealAmount"};
        ModelConvert[] converts={null,new CouponStatusConvert(),null,new InvestAddtimeConvert() ,new InvestAddtimeConvert(),null,null};

        List<String> cList= Arrays.asList(cArray);
        List<String> eList=Arrays.asList(eArray);
        List<ModelConvert> convertList=Arrays.asList(converts);
        Map map=new HashMap();
        map.put("ruleId", ruleId);
        List<BasCouponVo> contentList=couponService.getList(map);

        String serverDir=request.getServletContext().getRealPath("/")+"admin/export/";
        String excelFilePath=ExcelUtils.export("红包使用记录",cList,eList,convertList,contentList,serverDir);
        HashMap ret= Message.OK;
        ret.put("file","export/"+excelFilePath);

        return ret;
    }

    //导出领用的红包的使用记录
    @ResponseBody
    @RequestMapping(value = "/excel/ticketByRuleId", method = RequestMethod.POST)
    public Object excelTicket(Integer ruleId,HttpServletRequest request) {
        String[] cArray={"用户名","状态","加息劵比例","作用天数","领用时间","使用时间","投资项目","投资金额"};
        String[] eArray={"Username","Status","Rate","UsefulLife","Addtime","UseTime","ItemName","InvestDealAmount"};
        ModelConvert[] converts={null,new CouponStatusConvert(),null,null,new InvestAddtimeConvert() ,new InvestAddtimeConvert(),null,null};

        List<String> cList= Arrays.asList(cArray);
        List<String> eList=Arrays.asList(eArray);
        List<ModelConvert> convertList=Arrays.asList(converts);
        Map map=new HashMap();
        map.put("ruleId", ruleId);
        List<BasTicketVo> contentList=ticketService.getList(map);

        String serverDir=request.getServletContext().getRealPath("/")+"admin/export/";
        String excelFilePath=ExcelUtils.export("加息劵使用记录",cList,eList,convertList,contentList,serverDir);
        HashMap ret= Message.OK;
        ret.put("file","export/"+excelFilePath);

        return ret;
    }


}
